#   库的引用
import os.path
import datetime
import calendar

import PyQt6.QtWidgets as QtWidgets
import PyQt6.QtCore as QtCore
import PyQt6.QtGui as QtGui

#   添加任务
class AddTask(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.basePath = os.path.dirname(os.path.abspath(__file__))
        self.initUI()

    def initUI(self):       
        #   主Layout
        self.vboxMain = QtWidgets.QVBoxLayout()
        self.vboxMain.setContentsMargins(0, 0, 0, 0)
        self.vboxMain.setSpacing(6)
        self.setLayout(self.vboxMain)

        #   任务信息区
        self.infoWidget = QtWidgets.QFrame()
        #   设置主题
        self.stylePath = os.path.join(self.basePath, "Qss/add.qss")
        self.qssFile = open(self.stylePath, 'r')
        self.qssStyle = self.qssFile.read()
        self.infoWidget.setStyleSheet(self.qssStyle)
        self.vboxMain.addWidget(self.infoWidget)
        self.vboxInfo = QtWidgets.QVBoxLayout()
        self.vboxInfo.setContentsMargins(0, 0, 0, 0)
        self.vboxInfo.setSpacing(2)
        self.infoWidget.setLayout(self.vboxInfo)

        #   任务名称
        self.labName = QtWidgets.QLabel("任务名称：")
        self.vboxInfo.addWidget(self.labName)

        self.textName = QtWidgets.QLineEdit()
        self.vboxInfo.addWidget(self.textName)
        
        #   详细描述
        self.labDetail = QtWidgets.QLabel("详细描述：")
        self.vboxInfo.addWidget(self.labDetail)

        self.textDetail = QtWidgets.QLineEdit()
        self.textDetail.setPlaceholderText("选填")
        self.vboxInfo.addWidget(self.textDetail)

        #   任务时间
        self.labTime = QtWidgets.QLabel("任务时间：")
        self.vboxInfo.addWidget(self.labTime)
        self.today = datetime.datetime.today()
        self.year = str(self.today.year)
        self.month = str(self.today.month)
        self.day = str(self.today.day)

        self.timeWidget = QtWidgets.QFrame()
        self.vboxInfo.addWidget(self.timeWidget)
        self.vboxInfo.addStretch(1)
        self.hboxTime = QtWidgets.QHBoxLayout()
        self.hboxTime.setContentsMargins(0, 0, 0, 0)
        self.hboxTime.setSpacing(0)
        self.timeWidget.setLayout(self.hboxTime)

        self.textYear = QtWidgets.QLineEdit()
        self.textYear.setText(self.year)
        self.regYear = QtCore.QRegularExpression('^([1-2][0-9][0-9][0-9]|[3-9][0-9][0-9])$')
        self.yearValidator = QtGui.QRegularExpressionValidator(self.regYear)
        self.textYear.setValidator(self.yearValidator)
        self.textYear.textChanged.connect(self.setDay)
        self.hboxTime.addWidget(self.textYear)

        self.labYear = QtWidgets.QLabel("年")
        self.hboxTime.addWidget(self.labYear)

        self.textMonth = QtWidgets.QLineEdit()
        self.textMonth.setText(self.month)
        self.regMonth = QtCore.QRegularExpression('^([1-9]|1[0-2])$')
        self.monthValidator = QtGui.QRegularExpressionValidator(self.regMonth)
        self.textMonth.setValidator(self.monthValidator)
        self.textMonth.textChanged.connect(self.setDay)
        self.hboxTime.addWidget(self.textMonth)

        self.labMonth = QtWidgets.QLabel("月")
        self.hboxTime.addWidget(self.labMonth)

        self.textDay = QtWidgets.QLineEdit()
        self.textDay.setText(self.day)
        self.setDay()
        self.hboxTime.addWidget(self.textDay)

        self.labDay = QtWidgets.QLabel("日")
        self.hboxTime.addWidget(self.labDay)

        #   创建任务区
        self.btnCreate = QtWidgets.QPushButton("创建任务")
        self.vboxMain.addWidget(self.btnCreate)

    #   确定月份天数
    def setDay(self):
        if self.textYear.text() != "" and self.textMonth.text() != "":
            self.monthLenth = (calendar.monthrange(int(self.textYear.text()), int(self.textMonth.text())))[1]
        if self.textDay.text() != "" and int(self.textDay.text()) > self.monthLenth:
            self.textDay.clear()
            self.textDay.setStyleSheet("background: #ffb3a7")
            QtCore.QTimer.singleShot(500, self.initColor)
        if self.monthLenth < 30:
            self.regDay = QtCore.QRegularExpression('^([1-9]|1[0-9]|2[0-{0}])$'.format(self.monthLenth-20))
        else:
            self.regDay = QtCore.QRegularExpression('^([1-9]|1[0-0]|2[0-9]|3[0-{0}])$'.format(self.monthLenth-30))
        self.dayValidator = QtGui.QRegularExpressionValidator(self.regDay)
        self.textDay.setValidator(self.dayValidator)

    #   恢复日期窗口颜色，配合setDay()函数使用
    def initColor(self):
        self.textDay.setStyleSheet("background: white")

    #   初始化本窗口，在成功创建任务后使用
    def initAll(self):
        self.textName.clear()
        self.textDetail.clear()
        self.textYear.setText(self.year)
        self.textMonth.setText(self.month)
        self.textDay.setText(self.day)
        self.btnCreate.setText("创建任务")
        self.btnCreate.setStyleSheet("background: white")